//			state   index
//	待评价->  1	      0
//	已评价->  2	      1
//	被评价->  3	      2
//进入我的评价，更新所有数据，
//逻辑为：进入我的评价页面，发送ajax请求，得到数据，渲染所有页面，
//下拉刷新时判断新旧数组长度是否相同，相同则刷新此列表，否则不刷新。
var ele = document.querySelectorAll('.mui-table-view');
var oldData = [
	[],
	[],
	[]
];
updateData(ele[0], 0, oldData);
updateData(ele[1], 1, oldData);
updateData(ele[2], 2, oldData);

mui.init();
(function($) {
	//阻尼系数
	var deceleration = mui.os.ios ? 0.003 : 0.0009;
	$('.mui-scroll-wrapper').scroll({
		bounce: false,
		indicators: true, //是否显示滚动条
		deceleration: deceleration
	});
	$.ready(function() {
		$.each(document.querySelectorAll('.mui-slider-group .mui-scroll'), function(index, pullRefreshEl) { // index:0 1 2
			$(pullRefreshEl).pullToRefresh({
				down: {
					callback: function() {
						var self = this;
						setTimeout(function() {
							var ul = self.element.querySelector('.mui-table-view');
							updateData(ul, index, oldData[index]);
							self.endPullDownToRefresh();
						}, 600);
					}
				},
				up: {
					contentrefresh: "没有更多数据了",
					contentnomore: '没有更多数据了',
					callback: function() {
						var self = this;
						//setTimeout(function() {
						//var ul = self.element.querySelector('.mui-table-view');
						//updateData(ul, index, oldData[index]);
						self.endPullUpToRefresh(true);
						//}, 1000);
					}
				}
			});
		});

	});
})(mui);

function updateData(ele, index, oldData) {
	var url = 'https://bird.ioliu.cn/v1?url=http://app.xiaoweicanting.com/hospital.php?m=App&c=evaluation&a=myEvaluationList&hospitalid=10&departmentid=11&uid=3&token=dfaf&skip=1&limit=10&state=' + (index + 1);
	//与旧数据进行比较，若不同再刷新页面。
	mui.ajax(url, {
		dataType: 'json',
		type: 'get',
		success: function(data) {
			var listData = data.data,
				count = listData.length;
			if(listData.length === 0) {
				ele.innerHTML = '无数据';
			}
			//新旧数组长度相同，判断不需更新，返回true
			if(listData.length === oldData[index].length) {
				return true;
			}
			oldData[index] = listData;
			ele.innerHTML = '';
			ele.appendChild(createLi(ele, index, count, listData));
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			mui.toast('网络异常');
		}
	});
}

function createLi(ul, index, count, data) {
	var fragment = document.createDocumentFragment();
	var li;

	for(var i = 0; i < count; i++) {
		//  用户头像为空时，自动设置为 汉字取末尾，英文取首字母大写
		var myCell = data[i].addname.split("");
		var myCellNode;
		if(isChinese(data[i].addname)) {
			myCellNode = myCell[myCell.length - 1];
		} else {
			myCellNode = myCell[0].toUpperCase();
		}
		var addduimg = "";
		if(data[i].adduimg.length == 0) {
			adduimg =   "<div class=\"my-cell-title\">"+
							"<span class=\"circle-name\">" + myCellNode + "</span>"+
						"</div>"
		} else {
			adduimg = "<img src=\"http://app.xiaoweicanting.com/" + data[i].adduimg + "\" class=\"ic_head-img\" />"
		}

		// 加急标志设置 faultlevel 3->加急 2->紧急
		var faultlevel = "";
		if(data[i].faultlevel == 3) {
			faultlevel = "urgency-level-1";
		} else if(data[i].faultlevel == 2) {
			faultlevel = "urgency-level-2";
		}

		//生成星星评价html
		var starLi = "",
			starHtml = "",
			starLightNum = "";
		if(index !== 0) {
			switch(index) {
				case 1:
					starLightNum = data[i].hospitalStar;
					break;
				case 2:
					starLightNum = data[i].workerStar;
					break;
				default:
					console.log('index溢出')
					break;
			}
			for(var j = 0; j < 5; j++) {
				if(j < starLightNum) {
					starLi += "<li class=\"light\"></li> ";
				} else {
					starLi += "<li></li> ";
				}
			}
			starHtml = "<div class=\"starNum\" id=\"mystarNum\">"+
							"<ul>" + starLi + 
							"</ul>"+
						"</div>";
		}

		li = document.createElement('li');
		li.className = 'mui-table-view-cell';
		li.innerHTML =  "<div class=\"cell-info\">" +
							"<div class=\"info-left\">" + adduimg +
								"<div>" +
									"<span>" + data[i].addname + "</span>" +
									"<h5>科室: " + data[i].departmentname + "</h5>" +
								"</div>" +
							"</div>" +
							"<div class=\"info-right\">" +
								"<div class=\"info-level\">" + starHtml +
									"<div class=\" " + faultlevel + " \">" +
									"</div>" +
								"</div>" +
								"<div class=\"info-time text-grey\">" + data[i].addtime +
								"</div>" +
							"</div>" +
						"</div>" +
						"<div class=\"cell-content\">" +
							"<div class=\"content-title\">" + data[i].title +
							"</div>" +
							"<div class=\"content-details\">" + data[i].content + 
							"</div>" +
						"</div>"

		//				插入片段示例
		//				<div class="cell-info">
		//					<div class="info-left">
		//						<img src="../../images/drawable-hdpi/info_evaluate.png" class="ic_head-img" />
		//						<div>
		//							<span>李俊生</span>
		//							<h5>科室：内科</h5>
		//						</div>
		//					</div>
		//					<div class="info-right">
		//						<div class="info-level">
		//							<div class="starNum" id="mystarNum">
		//								<ul>
		//									<li class="light"></li>
		//									<li></li>
		//									<li></li>
		//									<li></li>
		//									<li></li>
		//								</ul>
		//							</div>
		//							<div class="urgency-level-1"></div>
		//						</div>
		//						<div class="info-time text-grey">
		//							2017-04-08 15:52:2
		//						</div>
		//					</div>
		//				</div>
		//
		//				<div class="cell-content">
		//					<div class="content-title">
		//						B超机不能扫描！B超机不能扫描！ B超机不能扫描！B超机不能扫描！
		//					</div>
		//					<div class="content-details">
		//						电源部分正常，扫描枪也正常，但是扫描没有图像显示，超急。 电源部分正常，扫描枪也正常，但是扫描没有图像显示，超急。
		//					</div>
		//				</div>

		// 采用向子页面传递数据的方式
		li.index = index;
		li.dataNum = i;
		li.addEventListener('tap', function() {
			var wv = mui.openWindow({
				url: 'ev_detail.html',
				id: 'ev_detail',
				extras: {
					index: this.index,
					data: data[this.dataNum]
				}
			});
		});
		fragment.appendChild(li);
	}
	return fragment;
};

function isChinese(text) {
	var reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
	if(reg.test(text)) {
		return true;
	} else {
		return false;
	}
}